package com.zyj.repairs.controller;

import java.util.List;

import com.zyj.assets.domain.AssetsInfo;
import com.zyj.assets.service.IAssetsInfoService;
import com.zyj.common.annotation.Log;
import com.zyj.common.core.controller.BaseController;
import com.zyj.common.core.domain.AjaxResult;
import com.zyj.common.core.domain.entity.SysRole;
import com.zyj.common.core.domain.entity.SysUser;
import com.zyj.common.core.page.TableDataInfo;
import com.zyj.common.enums.BusinessType;
import com.zyj.common.utils.poi.ExcelUtil;
import com.zyj.repairs.domain.AssetsRepairs;
import com.zyj.repairs.service.IAssetsRepairsService;
import com.zyj.supplier.service.IAsstesSupplierService;
import com.zyj.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 资产维修Controller
 * 
 * @author zhuyujie
 * @date 2022-05-12
 */
@Controller
@RequestMapping("/assets/repairs")
public class AssetsRepairsController extends BaseController
{
    private String prefix = "assets/repairs";

    @Autowired
    private IAssetsRepairsService assetsRepairsService;

    @Autowired
    private ISysUserService iSysUserServiceSysUserService;

    @Autowired
    private IAssetsInfoService assetsInfoService;

    @Autowired
    private IAsstesSupplierService asstesSupplierService;

    @GetMapping()
    public String repairs(ModelMap mmap)
    {
        List<SysRole> roles = getSysUser().getRoles();
        if(roles!=null && !roles.isEmpty()){
            int flag = 0;//是否是超管或维修员
            for(int i=0;i<roles.size();i++){
                SysRole role = roles.get(i);
                long rolesid = role.getRoleId();
                //判断是否管理员
                if(rolesid==4){
                    //判断是否维修员
                    flag = 1;
                    mmap.put("flag",flag);
                }
            }
        }

        return prefix + "/repairs";
    }

    /**
     * 查询资产维修列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AssetsRepairs assetsRepairs)
    {
        startPage();
        //获取登录用户角色
        List<SysRole> roles = getSysUser().getRoles();
        if(roles!=null && !roles.isEmpty()){
            Boolean flag = false;//是否是超管或维修员
            for(int i=0;i<roles.size();i++){
                SysRole role = roles.get(i);
                long rolesid = role.getRoleId();
                //判断是否管理员
                if(rolesid==1 || rolesid==3){
                    //判断是否管理员
                    flag = true;
                }else if(rolesid==4){
                    //判断是否维修员
                    flag = true;
                }
            }
            if(!flag){
                //普通员工
                assetsRepairs.setUserId(getUserId());
            }
        }

        List<AssetsRepairs> list = assetsRepairsService.selectAssetsRepairsList(assetsRepairs);
        return getDataTable(list);
    }

    /**
     * 导出资产维修列表
     */
    @Log(title = "资产维修", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AssetsRepairs assetsRepairs)
    {
        List<AssetsRepairs> list = assetsRepairsService.selectAssetsRepairsList(assetsRepairs);
        ExcelUtil<AssetsRepairs> util = new ExcelUtil<AssetsRepairs>(AssetsRepairs.class);
        return util.exportExcel(list, "资产维修数据");
    }

    /**
     * 新增资产维修
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 从员工新增资产维修
     */
    @GetMapping("/add/{id}")
    public String add(@PathVariable("id") String assetsid, ModelMap mmap)
    {
        SysUser user= iSysUserServiceSysUserService.selectUserById(getUserId());
        AssetsInfo info = assetsInfoService.selectAssetsInfoById(assetsid);
        mmap.put("assetsid", assetsid);
        mmap.put("userid", getUserId());
        mmap.put("username", user.getUserName());
        mmap.put("phone", user.getPhonenumber());
        mmap.put("assetsName", info.getAssetsName());
        mmap.put("assetsSn", info.getAssetsSn());
        mmap.put("roleId", getSysUser().getRoles().get(0).getRoleId());
        mmap.put("assetsSource", info.getAssetsSource());
        mmap.put("factoryId", info.getAssetsSource());
        mmap.put("factoryName", asstesSupplierService.selectAsstesSupplierBySupplierId(Long.parseLong(info.getAssetsSource())).getSupplierName());
        System.out.println(mmap);

        return prefix + "/add";
    }

    /**
     * 新增保存资产维修
     */
    @Log(title = "资产维修", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AssetsRepairs assetsRepairs)
    {
        assetsRepairs.setCreateBy(getLoginName());
        return toAjax(assetsRepairsService.insertAssetsRepairs(assetsRepairs));
    }

    /**
     * 修改资产维修
     */
    @GetMapping("/edit/{repairsId}")
    public String edit(@PathVariable("repairsId") Long repairsId, ModelMap mmap)
    {
        AssetsRepairs assetsRepairs = assetsRepairsService.selectAssetsRepairsByRepairsId(repairsId);
        mmap.put("repairsUserId", getUserId());
        mmap.put("repairsUserName", getSysUser().getUserName());
        mmap.put("assetsRepairs", assetsRepairs);
        return prefix + "/edit";
    }


    /**
     * 资产维修详情
     */
    @GetMapping("/detail/{repairsId}")
    public String detail(@PathVariable("repairsId") Long repairsId, ModelMap mmap)
    {
        AssetsRepairs assetsRepairs = assetsRepairsService.selectAssetsRepairsByRepairsId(repairsId);
        mmap.put("assetsRepairs", assetsRepairs);
        return prefix + "/detail";
    }

    /**
     * 修改保存资产维修
     */
    @Log(title = "资产维修", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AssetsRepairs assetsRepairs)
    {
        assetsRepairs.setCreateBy(getLoginName());
        return toAjax(assetsRepairsService.updateAssetsRepairs(assetsRepairs));
    }

    /**
     * 删除资产维修
     */
    @Log(title = "资产维修", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(assetsRepairsService.deleteAssetsRepairsByRepairsIds(ids));
    }
}
